package pw.yumc.Residence.commandsub;

import org.bukkit.Bukkit;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandException;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;

import com.bekvon.bukkit.residence.protection.ClaimedResidence;
import com.bekvon.bukkit.residence.protection.CuboidArea;
import com.bekvon.bukkit.residence.protection.ResidenceManager;
import com.sk89q.worldedit.bukkit.WorldEditPlugin;

import cn.citycraft.PluginHelper.commands.BaseCommand;
import pw.yumc.Residence.ResidenceMain;
import pw.yumc.Residence.selection.SelectionManager;
import pw.yumc.Residence.text.Language;

public class CommandArea extends BaseCommand {
    ResidenceMain plugin;

    public CommandArea(final ResidenceMain plugin) {
        super("a");
        this.plugin = plugin;
        setMinimumArguments(3);
        setOnlyPlayerExecutable();
        setPossibleArguments("<list|listall|add|remove|replace> <领地名称> [区域名称]");
    }

    @Override
    public void execute(final CommandSender sender, final Command command, final String label, final String[] args) throws CommandException {
        final Player player = (Player) sender;
        final boolean resadmin = (command != null);
        final ResidenceManager rmanager = plugin.getResidenceManager();
        final Language language = plugin.getLanguage();
        final SelectionManager smanager = plugin.getSelectionManager();
        final WorldEditPlugin wep = (WorldEditPlugin) Bukkit.getServer().getPluginManager().getPlugin("WorldEdit");
        int page = 1;
        try {
            if (args.length > 0) {
                page = Integer.parseInt(args[args.length - 1]);
            }
        } catch (final Exception ex) {
        }

        final String subcmd = args[0];

        final ClaimedResidence res = rmanager.getByName(args[1]);

        if (res == null) {
            player.sendMessage(ChatColor.RED + language.getPhrase("InvalidResidence"));
            return;
        }
        if (wep != null) {
            if (wep.getConfig().getInt("wand-item") == plugin.getConfigManager().getSelectionTooldID()) {
                smanager.worldEdit(player);
            }
        }
        if (args.length == 2) {
            switch (subcmd) {
            case "list":
                res.printAreaList(player, page);
                return;
            case "listall":
                res.printAdvancedAreaList(player, page);
                return;
            }
        }
        if (args.length == 3) {
            switch (subcmd) {
            case "add":
                if (smanager.hasPlacedBoth(player.getName())) {
                    res.addArea(player, new CuboidArea(smanager.getPlayerLoc1(player.getName()), smanager.getPlayerLoc2(player.getName())), args[2], resadmin);
                } else {
                    player.sendMessage(ChatColor.RED + language.getPhrase("SelectPoints"));
                }
                return;
            case "remove":
                res.removeArea(player, args[2], resadmin);
                return;
            case "replace":
                if (smanager.hasPlacedBoth(player.getName())) {
                    res.replaceArea(player, new CuboidArea(smanager.getPlayerLoc1(player.getName()), smanager.getPlayerLoc2(player.getName())), args[2], resadmin);
                } else {
                    player.sendMessage(ChatColor.RED + language.getPhrase("SelectPoints"));
                }
                return;
            }
        }
    }

}
